<template>
  <div class="public_main">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item v-if="$route.path=='/backmoneylists'">{{$t('sidebar.business')}}</el-breadcrumb-item>
        <el-breadcrumb-item v-else>{{$t('newcrumbs.crumbsOne')}}</el-breadcrumb-item>
        <el-breadcrumb-item>{{$t('loanMoney.crumbsTwo')}}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <!-- -------------搜索查询栏------------------------ -->
    <search :searchData="searchData" 
        @search="search" 
        @putExcel="putExcel" 
        :searchRight="true"
        :outputRight="$store.state.common.permiss[routename].includes('export')">
    </search>

    <!-- -------------表单显示栏------------------------ -->
    <div class="table" v-if="true">
      <template>
        <el-table :data="tableData" size="small" stripe v-loading="loadFlag">
        <el-table-column align="center" prop="orderId" :label="$t('public.orderId')">
          </el-table-column>
          <el-table-column align="center" prop="userId" :label="$t('public.userId')">
          </el-table-column>
          <el-table-column align="center" prop="user.name" :label="$t('public.name')">
          </el-table-column>
          <el-table-column align="center" prop="user.phone" :label="$t('public.userTel')">
          </el-table-column>
          <el-table-column align="center" prop="loanAmount" :label="$t('public.no30')">
            <template slot-scope="scope">
              <span v-if="scope.row.order.loanAmount!==null&&scope.row.order.loanAmount!==undefined&&scope.row.order.loanAmount!==''">{{$store.state.common.id_currency}}{{$store.getters.moneySplit(scope.row.order.loanAmount)}}{{$store.state.common.vi_currency}}</span>
              <span v-else>{{$store.state.common.nullData}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="productPeriod" :label="$t('public.no31')">
            <template slot-scope="scope">
              <span>{{scope.row.order.productPeriod}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="currentInterest" :label="$t('public.lastOverdueInterest')">
            <template slot-scope="scope">
              <span>{{$store.state.common.id_currency}}
                {{$store.getters.moneySplit(scope.row.order.overdueInterest)}}
                {{$store.state.common.vi_currency}}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" prop="overdueInterest" :label="$t('public.no56')">
            <template slot-scope="scope">
              <span v-if="scope.row.order.overdueInterest!==null&&scope.row.overdueInterest!==undefined&&scope.row.overdueInterest!==''">{{$store.state.common.id_currency}}{{$store.getters.moneySplit(scope.row.overdueInterest)}}{{$store.state.common.vi_currency}}</span>
              <span v-else>{{$store.state.common.nullData}}</span>
            </template>
          </el-table-column> -->
          <el-table-column align="center" prop="returnMoney" :label="$t('public.no27')" >
            <template slot-scope="scope">
              <span>{{$store.state.common.id_currency}}
                {{$store.getters.moneySplit(scope.row.order.loanAmount+scope.row.order.overdueInterest)}}
                {{$store.state.common.vi_currency}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="refundAmount" :label="$t('public.no57')">
            <template slot-scope="scope">
              <span v-if="scope.row.order.refundAmount!==null&&scope.row.order.refundAmount!==undefined&&scope.row.order.refundAmount!==''">{{$store.state.common.id_currency}}{{$store.getters.moneySplit(scope.row.order.refundAmount)}}{{$store.state.common.vi_currency}}</span>
              <span v-else>{{$store.state.common.nullData}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('public.no58')" width="86">
            <template slot-scope="scope">
              <span>{{scope.row.order.loanTime}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"  :label="$t('public.no59')" width="86">
            <template slot-scope="scope">
              <span>{{scope.row.order.mustRefundTime}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('public.no60')" width="86">
            <template slot-scope="scope">
              <span>{{scope.row.order.lastRefundTime}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="orderDeviceType" :label="$t('add.no7')">
            <template slot-scope="scope">
              <span>{{$t($store.getters.clientStatus(scope.row.user.regDevice))}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="overdueDays" :label="$t('public.no28')" >
            <template slot-scope="scope">
              <span>{{scope.row.order.overdueDays}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="status" :label="$t('public.orderStatus')" >
            <template slot-scope="scope">
              <span>{{$t($store.getters.backList_rejectStatus(scope.row.status))}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="isNew" :label="$t('add.no86')" >
            <template slot-scope="scope">
              <span>{{$t($store.getters.userTypeNewOldOther_status(scope.row.order.isNew))}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="appName" :label="$t('public.appName')" >
          </el-table-column>
          <el-table-column align="center" prop="appPackage" :label="$t('public.appPackage')" >
          </el-table-column>
          <el-table-column fixed="right" align="center" prop="operation" :label="$t('public.operation')" min-width="240">
            <template slot-scope="scope">
              <span v-if="$store.state.common.permiss[routename].includes('view')" class="table_opr" @click="detail(scope.row.orderId)">{{$t('public.detail')}}</span>
              <span 
                  v-if="$store.state.common.permiss[routename].includes('repay')&&scope.row.status!==51"
                  class="table_opr" 
                  @click="sure(scope.row.orderId,scope.row.userId)">
                  {{$t('loanMoney.repayMoney')}}
                </span>
              <!-- <span  v-if="$store.state.common.permiss[routename].includes('adjust')" class="table_opr" @click="modify(scope.row.id,scope.row.userId)">{{$t('finance.revise')}}</span> -->
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>

    <!-- ------------  分页显示栏  ------------------------ -->
    <el-row type="flex" justify="end">
      <div class="pages" >
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pagination.current"
          layout="sizes, prev, pager, next, total,->"
          :page-sizes="pagination.pageSizeOptions"
          :page-size="pagination.pageSize"
          @size-change="handleSizeChange"
          :total="pagination.total">
        </el-pagination>
      </div>
    </el-row>

    <!-- 确认还款弹窗 -->
    <el-dialog :title="$t('public.no61')" :visible.sync="repayVisible" @close="close" width="660px" :show-message="false">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="160px" size="medium">
        <el-form-item :label="$t('public.no57')" prop="repaymentAmount" >
          <el-input type="text" v-model="form.repaymentAmount"></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.no83')" prop="btId" >
          <el-input type="text" v-model="form.btId"></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.no60')" prop="repaymentTime">
           <el-date-picker
            size="small"
            v-model="form.repaymentTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            :placeholder="$t('public.backMoneyDate')">
          </el-date-picker>
        </el-form-item>
        <el-form-item :label="$t('public.orderStatus')"  prop="status" >
          <el-select v-model="form.status">
            <el-option :label="$t(item.label)" :value="item.value" v-for="(item, i) in options3" :key="i"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('finance.payType')" prop="payType">
          <el-select v-model="form.payType" :placeholder="$t('public.placeholder')">
            <el-option :label="$t(item.label)" :value="item.value" v-for="(item, i) in backChannel" :key="i"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('public.no37')" prop="remark" >
          <el-input type="textarea" v-model="form.remark" :rows="4" maxlength="600"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="sureSubmit('ruleForm')">{{$t('operationDetail.no22')}}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 修改弹窗 -->
    <el-dialog :title="$t('finance.revise')" :visible.sync="modifyFlag"  width="1200px" :show-message="false">
      <p style="font-weight:bold">{{$t('finance.log')}}</p>
      <el-table 
        :data="modifyData" 
        size="small" 
        style="width:100%" 
        highlight-current-row
        @current-change="tableRowChange">
        <el-table-column align="right" label="" width="60">
            <template slot-scope="scope">
                <el-radio v-model="radioVal" :label="scope.row.id">&nbsp;</el-radio>
            </template>
          </el-table-column>
        <el-table-column align="center" prop="id" :label="$t('public.backMoneyId')">
        </el-table-column>
        <el-table-column align="center" prop="btId" :label="$t('public.no83')">
        </el-table-column>
        <el-table-column align="center" prop="order.userName" :label="$t('public.name')" >
        </el-table-column>
        <el-table-column align="center" prop="order.userPhone" :label="$t('public.userTel')" >
        </el-table-column>
        <el-table-column align="center" prop="order.loanAmount" :label="$t('public.no54')">
          <template slot-scope="scope">
            <span v-if="scope.row.order.loanAmount!==null&&scope.row.order.loanAmount!==undefined&&scope.row.loanAmount!==''">{{$store.state.common.id_currency}}{{$store.getters.moneySplit(scope.row.order.loanAmount)}}{{$store.state.common.vi_currency}}</span>
            <span v-else>{{$store.state.common.nullData}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="order.productPeriod" :label="$t('public.no31')" >
        </el-table-column>
        <el-table-column align="center" prop="repaymentAmount" :label="$t('public.no65')" >
          <template slot-scope="scope">
            <span v-if="scope.row.repaymentAmount!==null&&scope.row.repaymentAmount!==undefined&&scope.row.repaymentAmount!==''">{{$store.state.common.id_currency}}{{$store.getters.moneySplit(scope.row.repaymentAmount)}}{{$store.state.common.vi_currency}}</span>
            <span v-else>{{$store.state.common.nullData}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="payType" :label="$t('finance.payType')">
        </el-table-column>
        <el-table-column align="center" prop="updateTime" :label="$t('public.no60')" width="86">
        </el-table-column>
        <el-table-column align="center" prop="status" :label="$t('add.no52')">
          <template >
            <span>{{$t('add.no53')}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="remark" :label="$t('public.no37')">
        </el-table-column>
      </el-table>
      <p style="font-weight:bold">{{$t('public.no51')}}</p>
      <el-form :model="modifyForm" label-width="140px" :inline="true" size="medium">
        <el-form-item :label="$t('public.no57')" >
          <el-input type="text" style="width:200px;" v-model="modifyForm.repaymentAmount"></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.no83')" >
          <el-input type="text" style="width:200px;" v-model="modifyForm.btId"></el-input>
        </el-form-item>
        <el-form-item :label="$t('finance.payType')">
          <el-select v-model="modifyForm.payType" :placeholder="$t('public.placeholder')">
            <el-option :label="$t(item.label)" :value="item.value" v-for="(item, i) in backChannel" :key="i"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('public.no60')" >
           <el-date-picker
            size="small"
            v-model="modifyForm.repaymentTime"
            type="datetime"
            :placeholder="$t('public.backMoneyDate')">
          </el-date-picker>
        </el-form-item>
        <el-form-item :label="$t('public.no37')" >
          <el-input type="textarea" style="width:400px;" v-model="modifyForm.remark" :rows="4" maxlength="600"></el-input>
        </el-form-item>
      </el-form>
      <div  class="dialog-footer">
        <!-- <el-button @click="modifyFlag = false">{{$t('public.no50')}}</el-button> -->
        <el-button type="primary" @click="modifySure">{{$t('public.no49')}}</el-button>
      </div>
    </el-dialog>
    
    <!-- 底部多出空白 -->
    <div class="foot"></div>

  </div>
</template>
<script>
import putExcel from 'src/mixins/putExcel.js';//导出excel
import getApp from 'src/mixins/getApp.js';//获取app和aok
import getMerchant from 'src/mixins/getMerchant.js';
export default {
  name: 'backMoneyList',
  mixins:[putExcel,getApp,getMerchant],
  data () {
    const searchData=[
      {
        type: 'input',
        label: 'public.orderId',
        attr: 'orderId',
        value: ''
      },
      {
        type: 'input',
        label: 'public.userId',
        attr: 'userId',
        value: ''
      },
      {
        type: 'input',
        label: 'public.name',
        attr: 'userName',
        value: ''
      },
      {
        type: 'input',
        label: 'public.userPhone',
        attr: 'userPhone',
        value: ''
      },{
        type: 'select',
        attr: 'status',
        label: 'public.orderStatus',
        option: this.$store.state.options.backMoney_options, // 订单状态下拉选框信息
        value: '',
        langFlag: true,
      },{
        type: 'select',
        attr: 'regDevice',
        label: 'add.no7',
        option: this.$store.state.options.loanDevice_options, // 借款客户端,
        value: '',
        langFlag: true,
      },{
        type: 'rangePicker',
        label: 'public.no59',
        attr: 'loanTime',
        value: [],
      },{
        type: 'rangePicker',
        label: 'public.no58',
        attr: 'mustRefundTime',
        value: [],
      },{
        type: 'rangePicker',
        label: 'public.no60',
        attr: 'lastRefundTime',
        value: [],
      },{
        type: 'doubleInput',
        label: 'public.no28',
        attr: ['overdueDaysMin','overdueDaysMax'],
        value: [],
      },{
        type: 'select',
        attr: 'appName',
        label: 'public.appName',
        option: [],
        value: '',
        langFlag: false,
      },{
        type: 'select',
        attr: 'appPackage',
        label: 'public.appPackage',
        option: [],
        value: '',
        langFlag: false,
      },{
        type: 'select',
        attr: 'isNew',
        label: 'add.no86',
        option: this.$store.state.options.userType_option,
        value: '',
        langFlag: true,
      },
      this.$route.name=='backmoneylists'?{
        type: 'select',
        attr: 'partnerId',
        label: 'proManage.partnerName',
        option: [], //商户名称
        value: '',
        langFlag: false,
      }:''
    ]
    return {
      flag: true,
      loadFlag: true,
      routename: this.$route.name,
      pagination: {
        pageSize: global.config.pageSize,
        pageSizeOptions: global.config.pageSizeOptions,
        current: 1,
        total: 0,
      },
      searchData:searchData,
      formInline: {// 用户查询信息数据对应字段
        orderId: '',
        userId: '',
        userName: '',
        userPhone:'',
        status:'',
        regDevice:'',
        appName:'',
        appPackage:'',
        loanTimeBegin:'',
        loanTimeEnd:'',
        mustRefundTimeBegin:'',
        mustRefundTimeEnd:'',
        lastRefundTimeBegin:'',
        lastRefundTimeEnd:'',
        isNew:'',
        overdueDaysMin:'',
        overdueDaysMax:''
      },
      options1: this.$store.state.options.backMoney_options, // 订单状态下拉选框信息
      options2: this.$store.state.options.isOverdue_option, // 逾期状态下拉选框信息
      options3: this.$store.state.options.backOrder_status, // 确认还款弹窗还款状态下拉选框信息
      options4: this.$store.state.options.loansType_options, // 贷款类型下拉选框信息
      options6: this.$store.state.options.loanDevice_options, // 借款客户端
      options10: this.$store.state.options.userType_option, // 用户类型
      putExcelUrl:this.$action.loan_repaymentExport,
      tableData: [],// 用户信息数据模拟
      repayVisible: false,// 确认还款弹窗开关
      orderNo_dialog: '',// 弹窗选中orderNo
      form: {
        id:'',
        adminId:'',
        repaymentAmount: '',
        repaymentTime: '',
        btId: '',
        status: '',
        payType: '',
        remark: '',
      },
      rules:{
        repaymentAmount: [
          { required:true, message: this.$t('login.required'), trigger: 'blur' }
        ],
        btId: [
          { required:true, message: this.$t('login.required'), trigger: 'blur' }
        ],
        repaymentTime: [
          { required: true, message: this.$t('login.required'), trigger: 'change' }
        ],
        status: [
          { required:true, message: this.$t('login.required'), trigger: 'change' }
        ],
        payType: [
          { required:true, message: this.$t('login.required'), trigger: 'change' }
        ]
      },
      modifyFlag: false,
      modifyData: [],
      modifyId: '',
      radioVal: '',
      modifyForm:{
        id: '',
        adminId:'',
        btId: '',
        repaymentAmount: '',
        repaymentTime: '',
        payType: '',
        remark: '',
      },
      backChannel: this.$store.state.options.backMoneyType_option_vi,// 还款渠道
    }
  },
  methods: {
    handleSizeChange (val) {// 每页条数变化时操作
      this.pagination.pageSize = val;
      this.backList();
    },
    handleCurrentChange (val) { // 分页按钮点击操作
      this.pagination.current = val;
      this.backList();
    },
    close(){
      this.repayVisible=false;
    },
    backList (val) { // 获取还款列表数据
      this.loadFlag = true;
      let option = {
        pageNum:this.pagination.current,
        pageSize:this.pagination.pageSize,
        param: {
          ...this.formInline
        }       
      }
      this.$axios.post(this.$action.loan_repaymentList, option).then(res => {
        this.flag = true;
        this.loadFlag = false;
        if (res.code == 200) {
          this.tableData = res.data.list;
          this.pagination.total = res.data.total;
        } else {
          this.$message.error(res.message);
        }
      })
    },
    search(search) { // 搜索
        this.formInline=search;
        this.formInline.loanTimeBegin=search.loanTime?search.loanTime[0]:'';
        this.formInline.loanTimeEnd=search.loanTime?search.loanTime[1]:'';
        this.formInline.mustRefundTimeBegin=search.mustRefundTime?search.mustRefundTime[0]:'';
        this.formInline.mustRefundTimeEnd=search.mustRefundTime?search.mustRefundTime[1]:'';
        this.formInline.lastRefundTimeBegin=search.lastRefundTime?search.lastRefundTime[0]:'';
        this.formInline.lastRefundTimeEnd=search.lastRefundTime?search.lastRefundTime[1]:'';
        if (this.flag) {
          this.flag = false;
          this.pagination.currentPage = 1;
          this.backList();
        }
    },
    sure (orderId,userId) { // 确认还款弹窗
      if (orderId) {
        this.form.id=orderId,
        this.form.adminId=userId,
        this.form.repaymentAmount='',
        this.form.repaymentTime='',
        this.form.btId='',
        this.form.status='',
        this.form.payType='',
        this.form.remark='',
        this.repayVisible = true;
      }
    },
    sureSubmit () {// 确认还款操作
      const self = this;
      self.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let option = {
            param: {
              ...self.form
            } 
          }
          this.$axios.post(this.$action.loan_repaymentSubmit, option).then(res => {
            if (res.code == 200) {
              self.$message.success(self.$t('message.success'));
              this.backList();
            }else{
              self.$message.error(res.message);
            }
          })
          this.repayVisible = false;
        } else {
          return false;
        }
      })
    },
    detail (orderId) { // 跳转详情页
      this.$router.push({path: '/loandetail', query: {orderId}});
    },
    modify(id,userId){
      this.modifyForm.adminId=userId;
      this.getModifyData(id);
    },
    getModifyData(id){
      this.$axios.post(this.$action.loan_repaymentReList+id).then(res => {
        this.modifyFlag = true;
        if (res.code == 200) {
          this.modifyData = res.data;
        }
      })
    },
    modifySure(){
      let option = {
        param: {
          ...this.modifyForm
        }
      }
      this.$axios.post(this.$action.loan_repaymentRevise, option).then(res => {
        if (res.code == 200) {
          this.$message.success(this.$t('message.success'));
          this.backList();
        }else{
          this.$message.error(res.message);
        }
      })
      this.modifyFlag = false;
    },
    tableRowChange (val) {
      this.radioVal = val.id;
      this.modifyForm.btId = val.btId;
      this.modifyForm.id = val.id;
      this.modifyForm.repaymentAmount = val.repaymentAmount;
      this.modifyForm.repaymentTime = val.updateTime;
      this.modifyForm.payType = val.payType;
      this.modifyForm.remark = val.remark;
    }
  },
  watch: {
    modifyFlag(){
      if(!this.modifyFlag){
        this.radioVal = '';
        this.modifyForm.btId = '';
        this.modifyForm.id = '';
        this.modifyForm.repaymentAmount = '';
        this.modifyForm.repaymentTime = '';
        this.modifyForm.payType = '';
        this.modifyForm.remark = '';
      }
    }
  },
  mounted () {
    this.sessionid = sessionStorage.getItem('sessionid');
    this.getApp().then(app=>{
      this.searchData[10].option=app.appName;
      this.searchData[11].option=app.appPackage;
    })
    if(this.$route.name=='backmoneylists'){
      this.getMerchant().then(arr=>{
        this.searchData[13].option=arr;
      })
    }
    this.backList();
  }
}
</script>
<style scoped lang="scss">

.el-dialog__body{
  padding-bottom: 0;
}
.dialog-footer{
  padding-left: 160px;
  text-align: left;
}


</style>
